<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="家属姓名" prop="relationName">
        <el-input
          v-model="queryParams.relationName"
          placeholder="请输入家属姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家属联系方式" prop="relationPhone">
        <el-input
          v-model="queryParams.relationPhone"
          placeholder="请输入家属姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="relationMail">
        <el-input
          v-model="queryParams.relationMail"
          placeholder="邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老人关系" prop="relationRelationship">
        <el-select
          v-model="queryParams.relationRelationship"
          placeholder="老人关系"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.user_cluster_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:hospitalization:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hospitalization:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hospitalization:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:hospitalization:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="hospitalizationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="家属姓名" align="center" prop="relationName" />
      <el-table-column label="家属联系方式" align="center" prop="relationPhone" />
      <el-table-column label="邮箱" align="center" prop="relationMail" />

      <el-table-column label="老人关系" align="center" prop="relationRelationship">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.user_cluster_type"
            :value="scope.row.relationRelationship"
          />
        </template>
      </el-table-column>

      <el-table-column label="现住址" align="center" prop="relationAddress" />
      <el-table-column label="性别" align="center" prop="relationGender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.relationGender" />
        </template>
      </el-table-column>

      <el-table-column label="是否是第一联系人" align="center" prop="relationFirstPerson">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.iot_yes_no"
            :value="scope.row.relationFirstPerson"
          />
        </template>
      </el-table-column>

      <el-table-column label="是否是担保人" align="center" prop="relationBondsman">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_yes_no" :value="scope.row.relationBondsman" />
        </template>
      </el-table-column>

      <el-table-column label="证件号码" align="center" prop="relationCertificatePhone" />
      <el-table-column label="工作单位" align="center" prop="relationWork" />
      <el-table-column label="经济状况" align="center" prop="relationFinancialSituation">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.user_money_type"
            :value="scope.row.relationFinancialSituation"
          />
        </template>
      </el-table-column>

      <el-table-column label="职务" align="center" prop="relationDuty" />
      <el-table-column label="备注" align="center" prop="relationRemark" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:hospitalization:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:hospitalization:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老人住院情况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row gutter="25">
          <el-col :span="12">
            <el-form-item label="家属姓名" prop="relationName">
              <el-input v-model="form.relationName" placeholder="请输入家属姓名" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="家属联系方式" prop="relationPhone">
              <el-input v-model="form.relationPhone" type="number" placeholder="请输入家属联系方式" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row gutter="25">
          <el-col :span="12">
            <el-form-item label="邮箱" prop="relationMail">
              <el-input v-model="form.relationMail" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="老人关系" prop="relationRelationship">
              <el-select
                v-model="form.relationRelationship"
                placeholder="请选择老人关系"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in dict.type.user_cluster_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value*1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row gutter="25">
          <el-col :span="12">
            <el-form-item label="现住址" prop="relationAddress">
              <el-input v-model="form.relationAddress" placeholder="请输入现住址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="relationGender">
              <el-select
                v-model="form.relationGender"
                placeholder="请选择性别"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row gutter="25">
          <el-col :span="12">
            <el-form-item label="第一联系人" prop="relationFirstPerson">
              <el-select
                v-model="form.relationFirstPerson"
                placeholder="请选择是否是第一联系人"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in dict.type.iot_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value*1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否是担保人" prop="relationBondsman">
              <el-select
                v-model="form.relationBondsman"
                placeholder="请选择是否是担保人"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in dict.type.iot_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value*1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row gutter="25">
          <el-col :span="12">
            <el-form-item label="证件号码" prop="relationCertificatePhone">
              <el-input
                v-model="form.relationCertificatePhone"
                placeholder="请输入证件号码"
                type="number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作单位" prop="relationWork">
              <el-input v-model="form.relationWork" placeholder="请输入工作单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row gutter="25">
          <el-col :span="12">
            <el-form-item label="经济状况" prop="relationFinancialSituation">
              <el-select
                v-model="form.relationFinancialSituation"
                placeholder="请选择经济状况"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in dict.type.user_money_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value*1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="relationDuty">
              <el-input v-model="form.relationDuty" placeholder="请输入工作职务" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row gutter="25">
          <el-col :span="12">
            <el-form-item label="备注" prop="relationRemark">
              <el-input v-model="form.relationRemark" placeholder="备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRelation,
  getRelation,
  addRelation,
  updateRelation,
  delRelation,
} from "@/api/cluster/relation";

export default {
  name: "Hospitalization",
  dicts: ["sys_user_sex", "iot_yes_no", "user_money_type", "user_cluster_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老人住院情况表格数据
      hospitalizationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oldManId: null,
        admissionDate: null,
        hospitalStays: null,
        inpatientInstitution: null,
        recoveryCondition: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        
        relationName: [{ required: true, message: "家属姓名不能为空", trigger: "blur" }],
        relationPhone: [{ required: true, message: "家属联系方式不能为空", trigger: "blur" }],
        relationPhone: [{ required: true, message: "邮箱不能为空", trigger: "blur" }],
        relationRelationship: [{ required: true, message: "老人关系不能为空", trigger: "change" }],
        relationAddress: [{ required: true, message: "现住址不能为空", trigger: "blur" }],
        relationGender: [{ required: true, message: "性别不能为空", trigger: "change" }],
        relationFirstPerson: [{ required: true, message: "第一联系人不能为空", trigger: "change" }],
        relationBondsman: [{ required: true, message: "是否是担保人不能为空", trigger: "change" }],
        relationCertificatePhone: [{ required: true, message: "证件号码不能为空", trigger: "blur" }],
        
      },
    };
  },
  created() {
    this.getList();
    console.log(this.$route);
  },
  methods: {
    /** 查询老人住院情况列表 */
    getList() {
      // console.log(this.$router);
      this.loading = true;
      this.queryParams.oldManId = this.$route.query.id;
      console.log(this.$route.query.id);
      listRelation(this.queryParams).then((response) => {
        this.hospitalizationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        admissionDate: null,
        hospitalStays: null,
        inpatientInstitution: null,
        recoveryCondition: null,
        relationName: null,
        relationPhone: null,
        relationMail: null,
        relationRelationship: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加老人家属信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRelation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老人家属信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRelation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.oldManId=Number(this.$route.query.id)
            addRelation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除老人家属信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRelation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/hospitalization/export",
        {
          ...this.queryParams,
        },
        `hospitalization_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
